package com.sensei.search.nodes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import proj.zoie.api.ZoieIndexReader;
import proj.zoie.api.indexing.IndexReaderDecorator;

import com.browseengine.bobo.api.BoboIndexReader;
import com.browseengine.bobo.facets.FacetHandler;
import com.browseengine.bobo.facets.FacetHandlerFactory;

public class SenseiIndexReaderDecorator implements IndexReaderDecorator<BoboIndexReader> {
	private final List<FacetHandlerFactory> _facetHandlerFactories;
	private static final Logger logger = Logger.getLogger(SenseiIndexReaderDecorator.class);
	
	public SenseiIndexReaderDecorator(List<FacetHandlerFactory> facetHandlerFactories)
	{
	  _facetHandlerFactories = facetHandlerFactories;
	}
	
	public SenseiIndexReaderDecorator()
	{
		this(null);
	}
	
	public BoboIndexReader decorate(ZoieIndexReader zoieReader) throws IOException {
	    if (zoieReader != null)
	    {
	    	logger.info("zoie reader is not null, maxdoc: "+zoieReader.maxDoc());
    		if (_facetHandlerFactories!=null)
    		{
    		  ArrayList<FacetHandler> handerList = new ArrayList<FacetHandler>(_facetHandlerFactories.size());
    	      for (FacetHandlerFactory factory : _facetHandlerFactories)
    	      {
    	        handerList.add(factory.newInstance());
    	      }
    	      return BoboIndexReader.getInstance(zoieReader,handerList);
    		}
    		else
    		{
    		   return BoboIndexReader.getInstance(zoieReader);
    		}
	    }
	    else
	    {
	    	logger.info("zoie reader is null");
	        return null;
	    }
	}
}

